//
//  ModelPostpone.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/18.
//

#import "ModelPostpone.h"

NSString *const kModelPostponeAmount = KEY_amount;
NSString *const kModelPostponeDelayServiceFee = KEY_delayServiceFee;
NSString *const kModelPostponeDelayExpiryTime = KEY_delayExpiryTime;
NSString *const kModelPostponeLoanAmount = KEY_loanAmount;
NSString *const kModelPostponeRepayAmount = KEY_repayAmount;
NSString *const kModelPostponeOrderNo = KEY_orderNo;
NSString *const kModelPostponeTermUnit = KEY_termUnit;
NSString *const kModelPostponeExtensionDetails = KEY_extensionDetails;
NSString *const kModelPostponeDelayNum = KEY_delayNum;
NSString *const kModelPostponeDelayTotalNum = KEY_delayTotalNum;
NSString *const kModelPostponeDelayTerm = KEY_delayTerm;
NSString *const kModelPostponeRepaymentPlans = KEY_repaymentPlans;

@interface ModelPostpone ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelPostpone

@synthesize amount = _amount;
@synthesize delayServiceFee = _delayServiceFee;
@synthesize delayExpiryTime = _delayExpiryTime;
@synthesize loanAmount = _loanAmount;
@synthesize repayAmount = _repayAmount;
@synthesize orderNo = _orderNo;
@synthesize termUnit = _termUnit;
@synthesize extensionDetails = _extensionDetails;
@synthesize delayNum = _delayNum;
@synthesize delayTotalNum = _delayTotalNum;
@synthesize delayTerm = _delayTerm;
@synthesize repaymentPlans = _repaymentPlans;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.amount = [self objectOrNilForKey:kModelPostponeAmount fromDictionary:dict];
        self.delayServiceFee = [self objectOrNilForKey:kModelPostponeDelayServiceFee fromDictionary:dict];
        self.delayExpiryTime = [self objectOrNilForKey:kModelPostponeDelayExpiryTime fromDictionary:dict];
        self.loanAmount = [self objectOrNilForKey:kModelPostponeLoanAmount fromDictionary:dict];
        self.repayAmount = [[self objectOrNilForKey:kModelPostponeRepayAmount fromDictionary:dict] doubleValue];
        self.orderNo = [self objectOrNilForKey:kModelPostponeOrderNo fromDictionary:dict];
        self.termUnit = [[self objectOrNilForKey:kModelPostponeTermUnit fromDictionary:dict] doubleValue];
        
        NSObject *receivedExtensionDetails = [dict objectForKey:kModelPostponeExtensionDetails];
        NSMutableArray *parsedExtensionDetails = [NSMutableArray array];
        if ([receivedExtensionDetails isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedExtensionDetails) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedExtensionDetails addObject:[ModelPairs modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedExtensionDetails isKindOfClass:[NSDictionary class]]) {
            [parsedExtensionDetails addObject:[ModelPairs modelObjectWithDictionary:(NSDictionary *)receivedExtensionDetails]];
        }
        self.extensionDetails = [NSArray arrayWithArray:parsedExtensionDetails];
        
        NSObject *receivedRepaymentPlans = [dict objectForKey:kModelPostponeRepaymentPlans];
        NSMutableArray *parsedRepaymentPlans = [NSMutableArray array];
        if ([receivedRepaymentPlans isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedRepaymentPlans) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedRepaymentPlans addObject:[ModelPairs modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedRepaymentPlans isKindOfClass:[NSDictionary class]]) {
            [parsedRepaymentPlans addObject:[ModelPairs modelObjectWithDictionary:(NSDictionary *)receivedRepaymentPlans]];
        }
        self.repaymentPlans = [NSArray arrayWithArray:parsedRepaymentPlans];
        
        self.delayNum = [[self objectOrNilForKey:kModelPostponeDelayNum fromDictionary:dict] doubleValue];
        self.delayTotalNum = [[self objectOrNilForKey:kModelPostponeDelayTotalNum fromDictionary:dict] doubleValue];
        self.delayTerm = [ModelDelayTerm modelObjectWithDictionary:[dict objectForKey:kModelPostponeDelayTerm]];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.amount forKey:kModelPostponeAmount];
    [mutableDict setValue:self.delayServiceFee forKey:kModelPostponeDelayServiceFee];
    [mutableDict setValue:self.delayExpiryTime forKey:kModelPostponeDelayExpiryTime];
    [mutableDict setValue:self.loanAmount forKey:kModelPostponeLoanAmount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.repayAmount] forKey:kModelPostponeRepayAmount];
    [mutableDict setValue:self.orderNo forKey:kModelPostponeOrderNo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.termUnit] forKey:kModelPostponeTermUnit];
    NSMutableArray *tempArrayForExtensionDetails = [NSMutableArray array];
    for (NSObject *subArrayObject in self.extensionDetails) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            [tempArrayForExtensionDetails addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            [tempArrayForExtensionDetails addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForExtensionDetails] forKey:kModelPostponeExtensionDetails];
    [mutableDict setValue:[NSNumber numberWithDouble:self.delayNum] forKey:kModelPostponeDelayNum];
    [mutableDict setValue:[NSNumber numberWithDouble:self.delayTotalNum] forKey:kModelPostponeDelayTotalNum];
    [mutableDict setValue:[self.delayTerm dictionaryRepresentation] forKey:kModelPostponeDelayTerm];
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


@end
